

package com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.skytech.tax.customsdatawarehouse.common.util.SecurityUtil;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.dao.ManualGoodsConDao;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.dao.ManualGoodsExgDao;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.dao.ManualGoodsImgDao;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.entity.ManualGoodsCon;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.entity.ManualGoodsExg;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.entity.ManualGoodsImg;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lianhong
 * @since 2021/11/11
 */
@Service
public class ManualGoodsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ManualGoodsService.class);

    @Value("${customsdatawarehouse.token}")
    private String token;

    @Resource
    private ManualGoodsConDao manualGoodsConDao;
    @Resource
    private ManualGoodsExgDao manualGoodsExgDao;
    @Resource
    private ManualGoodsImgDao manualGoodsImgDao;

    public String saveCon(String con) {
        if (StringUtils.isEmpty(con)) return StringUtils.EMPTY;
        String conText = SecurityUtil.decrypt3DES(token, con);
        try {
            JSONObject c = JSON.parseObject(conText);
            if (c == null) return StringUtils.EMPTY;

            String seqNo = c.getString("seqNo");
            JSONArray conArr = c.getJSONArray("con");

            if (StringUtils.isEmpty(seqNo)) return StringUtils.EMPTY;
            if (CollectionUtils.isEmpty(conArr)) return StringUtils.EMPTY;

            List<String> gseqNoList = conArr.stream()
                    .map(item -> ((JSONObject) item).getString("gseqNo"))
                    .collect(Collectors.toList());

            Lists.partition(gseqNoList, 1000)
                    .forEach(gseqNos -> manualGoodsConDao.removeBySeqNoAndGseqNoIn(seqNo, gseqNos));

            List<ManualGoodsCon> manualGoodsConList = new ArrayList<>();
            for (int i = 0; i < conArr.size(); i++) {
                JSONObject item = conArr.getJSONObject(i);
                manualGoodsConList.add(new ManualGoodsCon(seqNo, item.getString("gseqNo"), item.toJSONString()));
            }

            Lists.partition(manualGoodsConList, 1000)
                    .forEach(items -> manualGoodsConDao.save(items));

            return SecurityUtil.encrypt3DES(token, seqNo);
        } catch (Exception e) {
            LOGGER.error("保存加工贸易手册单耗错误", e);
            return StringUtils.EMPTY;
        }
    }

    public String saveExg(String exg) {
        if (StringUtils.isEmpty(exg)) return StringUtils.EMPTY;
        String exgText = SecurityUtil.decrypt3DES(token, exg);
        try {
            JSONObject c = JSON.parseObject(exgText);
            if (c == null) return StringUtils.EMPTY;

            String seqNo = c.getString("seqNo");
            JSONArray exgArr = c.getJSONArray("exg");

            if (StringUtils.isEmpty(seqNo)) return StringUtils.EMPTY;
            if (CollectionUtils.isEmpty(exgArr)) return StringUtils.EMPTY;

            List<String> gdsSeqnoList = exgArr.stream()
                    .map(item -> ((JSONObject) item).getString("gdsSeqno"))
                    .collect(Collectors.toList());

            Lists.partition(gdsSeqnoList, 1000)
                    .forEach(gdsSeqnos -> manualGoodsExgDao.removeBySeqNoAndGdsSeqnoIn(seqNo, gdsSeqnos));

            List<ManualGoodsExg> manualGoodsExgList = new ArrayList<>();
            for (int i = 0; i < exgArr.size(); i++) {
                JSONObject item = exgArr.getJSONObject(i);
                manualGoodsExgList.add(new ManualGoodsExg(seqNo, item.getString("gdsSeqno"), item.toJSONString()));
            }

            Lists.partition(manualGoodsExgList, 1000)
                    .forEach(items -> manualGoodsExgDao.save(items));

            return SecurityUtil.encrypt3DES(token, seqNo);
        } catch (Exception e) {
            LOGGER.error("保存加工贸易手册成品错误", e);
            return StringUtils.EMPTY;
        }
    }

    public String saveImg(String img) {
        if (StringUtils.isEmpty(img)) return StringUtils.EMPTY;
        String imgText = SecurityUtil.decrypt3DES(token, img);
        try {
            JSONObject c = JSON.parseObject(imgText);
            if (c == null) return StringUtils.EMPTY;

            String seqNo = c.getString("seqNo");
            JSONArray imgArr = c.getJSONArray("exg");

            if (StringUtils.isEmpty(seqNo)) return StringUtils.EMPTY;
            if (CollectionUtils.isEmpty(imgArr)) return StringUtils.EMPTY;

            List<String> gdsSeqnoList = imgArr.stream()
                    .map(item -> ((JSONObject) item).getString("gdsSeqno"))
                    .collect(Collectors.toList());

            Lists.partition(gdsSeqnoList, 1000)
                    .forEach(gdsSeqnos -> manualGoodsImgDao.removeBySeqNoAndGdsSeqnoIn(seqNo, gdsSeqnos));

            List<ManualGoodsImg> manualGoodsImgList = new ArrayList<>();
            for (int i = 0; i < imgArr.size(); i++) {
                JSONObject item = imgArr.getJSONObject(i);
                manualGoodsImgList.add(new ManualGoodsImg(seqNo, item.getString("gdsSeqno"), item.toJSONString()));
            }

            Lists.partition(manualGoodsImgList, 1000)
                    .forEach(items -> manualGoodsImgDao.save(items));

            return SecurityUtil.encrypt3DES(token, seqNo);
        } catch (Exception e) {
            LOGGER.error("保存加工贸易手册料件错误", e);
            return StringUtils.EMPTY;
        }
    }
}
